import java.util.concurrent.*;

/**
 * @Author: 胡成
 * @Version: 0.0.1V
 * @Date: 2018/8/10
 * @Description: 类描述
 **/
public class Test {
    public static void main(String[] args) throws Exception {
        final ExecutorService exec = Executors.newFixedThreadPool(1);
        Callable<String> call = () -> {
            //开始执行耗时操作
            Thread.sleep(1000 * 5);
            return "线程执行完成.";
        };
        try {
            Future<String> future = exec.submit(call);
            //任务处理超时时间设为 1 秒
            String obj = future.get(1000, TimeUnit.MILLISECONDS);
            System.out.println("任务成功返回:" + obj);
        } catch (TimeoutException ex) {
            System.out.println("处理超时啦....");
            throw new Exception("调用超时");
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 关闭线程池
        exec.shutdown();
    }
}
